package com.villaday.idf.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.villaday.idf.R;
import com.villaday.idf.model.AreaListCopyEntity;

import java.util.List;


/**
 * Created by Administrator on 2017/3/20 0020.
 */
public class ChoseCityLVAdapter extends BaseAdapter implements View.OnClickListener {


    private android.content.Context Context;
    private List<AreaListCopyEntity> areaListCopyEntity;
    private ViewHolder viewHolder;
    private TextOnClick textOnClick;

    public ChoseCityLVAdapter(android.content.Context context, List<AreaListCopyEntity> areaListCopyEntity) {

        this.Context = context;
        this.areaListCopyEntity = areaListCopyEntity;
    }

    @Override
    public int getCount() {
        return areaListCopyEntity.size();
    }

    @Override
    public Object getItem(int position) {
        return areaListCopyEntity.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(Context).inflate(R.layout.item_chose_city_head, null);
            viewHolder.ll = (LinearLayout) convertView.findViewById(R.id.ll_choseCity);
            viewHolder.tvLetter = (TextView) convertView.findViewById(R.id.tv_choseCity_firstChar);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_choseCity_name);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_choseCity_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.ll.setVisibility(View.GONE);
        viewHolder.tvLetter.setText("");
        viewHolder.tvName.setText("");
        viewHolder.tvName.setTag(position);
        viewHolder.tvName.setOnClickListener(this);
        if (position==0){
            viewHolder.ll.setVisibility(View.VISIBLE);

        }else if (areaListCopyEntity.get(position).getFirstChar().substring(0,1).toUpperCase().equals(areaListCopyEntity.get(position-1).getFirstChar().substring(0,1).toUpperCase())){
            viewHolder.ll.setVisibility(View.GONE);

        }else {
            viewHolder.ll.setVisibility(View.VISIBLE);
        }

        viewHolder.tvLetter.setText(areaListCopyEntity.get(position).getFirstChar().substring(0,1).toUpperCase());
        viewHolder.tvName.setText(areaListCopyEntity.get(position).getAreaName());

        return convertView;
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = areaListCopyEntity.get(i).getFirstChar();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public void onClick(View v) {
        int posion = (int) v.getTag();
        textOnClick.textOnClick(posion);

    }

    final static class ViewHolder {
        TextView tvLetter;
        TextView tvName;
        LinearLayout ll;
    }

    public void setTextOnClick(TextOnClick textOnClick){

        this.textOnClick = textOnClick;
    }

    public interface TextOnClick{
        void textOnClick(int position);
    }
}
